package com.tencent.txcc.web.controller;

import com.tencent.txcc.dto.SmsCodeDto;
import com.tencent.txcc.result.JSONResult;
import com.tencent.txcc.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/verifycode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/imageCode/{key}")
    public JSONResult image(@PathVariable("key") String imageCodeKey) {
        try {
            // 获取64位图形验证码，即64位编码字符串
            String code64 = verifyCodeService.image(imageCodeKey);
            // 将获得的编码响应给前端
            return JSONResult.success(code64);
        } catch (Exception e) {
            e.printStackTrace();
            // 失败则返回失败原因
            return JSONResult.error(false, "图形验证码获取失败");
        }
    }

    @PostMapping("/sendSmsCode")
    public JSONResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto) {

        verifyCodeService.sendSmsCode(smsCodeDto);
        // 将获得的编码响应给前端
        return JSONResult.success();

    }
}
